package com.ushier.shop.drugstore.controller;

import com.ushier.shop.drugstore.bean.ResponseMessageBean;
import com.ushier.shop.drugstore.bean.User;
import com.ushier.shop.drugstore.dao.UserDAO;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/manage/user")
public class ManageUserServlet extends HttpServlet {

    private UserDAO userDAO = new UserDAO();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("content-type","text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        String action = req.getParameter("action");

        switch (action){
            case "list":
                List<User> list = userDAO.list();
                out.print(JSONObject.fromObject(ResponseMessageBean.success(list)));
                return;
            default:
                break;
        }


    }
}
